package com.liyujie.dyeingmachine.ui.start;


import android.os.Bundle;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ActivityStartBinding;
import com.liyujie.dyeingmachine.mobile.dao.CylinderBlockEntityDao;
import com.liyujie.dyeingmachine.mobile.entity.CylinderBlockEntity;
import com.liyujie.dyeingmachine.ui.BaseActivity;
import com.liyujie.dyeingmachine.ui.home.HomeActivity;
import com.liyujie.dyeingmachine.utils.PermissionsUtils;

import java.util.List;

public class StartActivity extends BaseActivity<ActivityStartBinding> {


    @Override
    public void initCreate() {
        super.initCreate();
        setTheme(R.style.NoStatusTheme1);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_start;

    }


    @Override
    public void initData() {
        super.initData();
        //获取全部数量
        long mFullQuantity = CylinderBlockEntityDao.getInstance().GetFullQuantity();
        if (mFullQuantity == 0) {
            for (int i = 0; i < 21; i++) {
                CylinderBlockEntity mCylinderBlockEntity = new CylinderBlockEntity();
                //缸体编号
                mCylinderBlockEntity.CylinderBlockNumber = (i + 1);
                //添加
                mCylinderBlockEntity.save();
            }
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        if (PermissionsUtils.setAuthority(mContext)) {
            /**
             *
             */
            binding.PutOff.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ActivityUtils.startActivity(HomeActivity.class);
                    ActivityUtils.finishActivity(StartActivity.class);
                }
            }, 2000);
        } else {
            XXPermissions.with(mContext).permission(Permission.MANAGE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
                @Override
                public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                    if (allGranted) {
                        ActivityUtils.startActivity(HomeActivity.class);
                        ActivityUtils.finishActivity(StartActivity.class);
                    } else {
                        ToastUtils.showShort("部分权限未正常授予");
                    }

                }

                @Override
                public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                    OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                    if (doNotAskAgain) {
                        ToastUtils.showShort("被永久拒绝授权，请手动授予存储权限");
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(StartActivity.this, permissions);
                    } else {
                        ToastUtils.showShort("获取存储权限失败");
                    }
                }
            });
        }


    }
}
